﻿'
' Copyright (c) 2010-2012, MatthiWare
'
' Permission is hereby granted, free of charge, to any person obtaining a
' copy of this software and associated documentation files (the "Software"),
' to deal in the Software without restriction, including without limitation
' the rights to use, copy, modify, merge, publish, distribute, sublicense,
' and/or sell copies of the Software, and to permit persons to whom the
' Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in
' all copies or substantial portions of the Software. 
' You shall include 'MatthiWare' in the credits/about section of the program
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
' FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
'

Public Class Inventory

    Public selectedSlot As InventorySlot = Nothing
    Public selectedIndex As Int32 = 0

    Public show As Boolean = False

    Public Shared SlotsWidth As Int32 = 10
    Public Shared SlotsHeight As Int32 = 4

    Public slots(SlotsWidth, SlotsHeight) As InventorySlot

    Public mouseSelected As InventorySlot = Nothing
    Public mouseOver As InventorySlot = Nothing

    Public Sub Up()
        If (selectedIndex = 0) Then
            selectedIndex = 9
        Else
            selectedIndex -= 1
        End If
        selectedSlot = slots(selectedIndex, 0)
    End Sub

    Public Sub Down()
        If (selectedIndex = 9) Then
            selectedIndex = 0
        Else
            selectedIndex += 1
        End If
        selectedSlot = slots(selectedIndex, 0)
    End Sub

    Public Sub New(e As Core.Base.GameCoreEventArgs)
        For x As Int32 = 0 To SlotsWidth - 1
            For y As Int32 = 0 To SlotsHeight - 1
                slots(x, y) = New InventorySlot(x, y, e)
            Next
        Next
        selectedSlot = slots(selectedIndex, 0)
    End Sub

    Public Sub Render(e As Core.Base.DrawEventArgs, showp As Boolean)
        Me.show = showp
        For x As Int32 = 0 To SlotsWidth - 1
            If (Me.show) Then
                For y As Int32 = 0 To (SlotsHeight - 1)
                    Dim s As InventorySlot = slots(x, y)
                    If (Me.selectedSlot.X = s.X And Me.selectedSlot.Y = s.Y) Then
                        s.Render(e, True)
                    Else
                        s.Render(e, False)
                    End If
                Next
#If Not Debug Then
                If (Not IsNothing(mouseOver)) Then
                    e.Target.DrawText(mouseOver.Info.ToString(), New Font("Comic Sans MS", 15, FontStyle.Bold, GraphicsUnit.Pixel), Brushes.White, frmMain.Current.X + 12, frmMain.Current.Y + 15)
                End If
#End If
            Else
                Dim s As InventorySlot = slots(x, 0)
                If (Me.selectedSlot.X = s.X And Me.selectedSlot.Y = s.Y) Then
                    s.Render(e, True)
                Else
                    s.Render(e, False)
                End If
            End If
        Next
    End Sub

    Public Sub Switch(pt1 As Point, pt2 As Point)
        Dim tmpInvSlot As InventorySlot = slots(pt1.X, pt1.Y)
        slots(pt1.X, pt1.Y) = slots(pt2.X, pt2.Y)
        slots(pt2.X, pt2.Y) = tmpInvSlot
    End Sub

    Public Sub Switch(s1 As InventorySlot, s2 As InventorySlot)
        Dim tmpInfo As InvSlotInfo = s1.Info
        s1.Info = s2.Info
        s2.Info = tmpInfo
    End Sub

    Public Sub Tick(e As Core.Base.GameCoreEventArgs)
#If Not Debug Then


        Dim p As Player = e.Core.Services.GetService(GetType(Player))
        Dim Myx As Int32 = Math.Floor(frmMain.Current.X / InventorySlot.INV_WIDHT_S)
        Dim Myy As Int32 = Math.Floor((frmMain.Current.Y - 25) / InventorySlot.INV_HEIGHT_S)

        If (Myx < SlotsWidth And Myx >= 0 And Myy < SlotsHeight And Myy >= 0) Then
            Dim s As InventorySlot = slots(Myx, Myy)
            mouseOver = s

            If (p.leftMouseDown) Then
                If (IsNothing(mouseSelected)) Then
                    mouseSelected = s
                Else
                    If ((s.Y > 0 And show) Or (s.Y = 0)) Then
                        Switch(s, mouseSelected)
                        mouseSelected = Nothing
                    End If

                End If
                If (s.Y = 0) Then
                    selectedSlot = s
                    selectedIndex = s.X
                End If
            End If

        Else
            mouseOver = Nothing
        End If
#End If
    End Sub

    Public Function GetSlotFor(t As Integer) As InventorySlot
        Dim poss As New List(Of InventorySlot)

        For x As Int32 = 0 To SlotsWidth - 1
            For y As Int32 = 0 To SlotsHeight - 1
                Dim invSlot As InventorySlot = slots(x, y)
                If (invSlot.Info.Item.IsStackable) Then
                    If (invSlot.Info.Stack < 99) And ((invSlot.Info.Item.Type = t) Or (invSlot.Info.Item.Type = 0)) Then
                        ' Same type and not full yet
                        poss.Add(invSlot)
                    End If
                Else
                    If ((invSlot.Info.Stack = 0) And (invSlot.Info.Item.Type = 0)) Then
                        poss.Add(invSlot)
                    End If
                End If
            Next
        Next

        For Each i As InventorySlot In poss.ToArray()
            If (i.Info.Item.IsStackable) Then
                If (i.Info.Item.Type = t) Then
                    Return i
                End If
            Else
                If (Not i.Info.Item.Type = t) Then
                    Return i
                End If
            End If
        Next

        Return poss(0)

    End Function



End Class
